#ifndef ANALYSISXMLREFERENCE_HPP
#define ANALYSISXMLREFERENCE_HPP

#include "analysis/AnalysisXML.hpp"
#include "store/struct/Reference.hpp"
#include "Utility.hpp"
#include <QString>

// 特化 AnalysisXML<Reference> 用于解析 BeatEditor.xml
template <>
struct AnalysisXML<Reference> {
    static Reference fromXMLElement(tinyxml2::XMLElement* elem) {
        Reference reference;
        const char* nodeName = elem->Attribute("name");
        reference.node = nodeName ? QString::fromUtf8(nodeName) : QString();

        tinyxml2::XMLElement* startElem = elem->FirstChildElement("start");
        if (startElem) {
            const char* sName  = startElem->Attribute("name");
            const char* sType  = startElem->Attribute("type");
            const char* sValue = startElem->Attribute("value");
            const char* sBoard = startElem->Attribute("board");
            const char* sSlot  = startElem->Attribute("slot");

            reference.sname  = sName  ? QString::fromUtf8(sName)  : QString();
            reference.stype  = parseType(sType);
            reference.svalue = parseValue(sValue);
            reference.sboard = sBoard ? QString::fromUtf8(sBoard) : QString();
            reference.sslot  = sSlot  ? QString::fromUtf8(sSlot)  : QString();
        }

        tinyxml2::XMLElement* endElem = elem->FirstChildElement("end");
        if (endElem) {
            const char* eName  = endElem->Attribute("name");
            const char* eType  = endElem->Attribute("type");
            const char* eValue = endElem->Attribute("value");
            const char* eBoard = endElem->Attribute("board");
            const char* eSlot  = endElem->Attribute("slot");

            reference.ename  = eName  ? QString::fromUtf8(eName)  : QString();
            reference.etype  = parseType(eType);
            reference.evalue = parseValue(eValue);
            reference.eboard = eBoard ? QString::fromUtf8(eBoard) : QString();
            reference.eslot  = eSlot  ? QString::fromUtf8(eSlot)  : QString();
        }

        return reference;
    }
};

#endif // ANALYSISXMLREFERENCE_HPP
